<?php
// picture uploader (get file from UI, save to server)
//
// digiboard by digitalboom.org
// http://www.digitalboom.org
//
// Information Technology program
// Sirindhorn International Institute of Technology
// Thammasat University, Rangsit Campus
// http://www.siit.tu.ac.th
//
// first created on: 2001 xxx xx  by Oil it://3
// last modified on: 2005 Aug 04 06 by bact'

include("settings/config.inc.php");

extract($_POST);
extract($_GET);

$FULL_UPLOAD_PATH = $DGB["HTTP_PATH"]."/".$DGB["UPLOAD_PATH"];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?=$DGB["ENCODING"];?>">
<title>picture uploader (<?=$Topic_ID;?>)</title>
<link rel="stylesheet" href="<?=$DGB["STYLESHEET"];?>" type="text/css">
</head>

<body bgcolor="#FFFFFF">
<?php
if ($DGB["ENABLE_UPLOAD"]==true)  {
 $count = 0;
 if (!isset($tcount)) 
  $tcount = 0;
?>

<table width="100%" border=0 cellspacing=0 cellpadding=0 valign=top align="center">
<tr bgcolor="#999999">
<td><img src="images/i_upload.gif" width=19 height=19 align="middle"> <font color="#ffffff"><b>picture uploader</b></font></td>
</tr>
 <tr>
  <td><font size=2><br>&nbsp;click '<font color="#006600">picture name</font>' to put (paste) it to
  <?if( $Topic_ID == "0000" ) {?>
  new topic
  <?} else {?>
   topic <?=$Topic_ID;?>
  <?}?>'s message :</td>
 </tr>
<tr>
<td>
 <font size=2><br>
  <?php
  $tmp_tcount = $tcount;
  for ($i=0; $i<$DGB["UploadMaxNum"]; $i++) {
   if (!empty($_FILES['userfile']['name'][$i])) {
    $size = $_FILES['userfile']['size'][$i];
	echo "&nbsp;".($i+1+$tmp_tcount).") ";
    if ($size <= 0) {
     echo "[<b>x</b>] <font color=\"#990000\">Empty file. Not uploaded.</font><br><br>";
    } elseif (!in_array($_FILES['userfile']['type'][$i], $DGB["UploadTypeAllowed"])) {
     echo "[<b>x</b>] <font color=\"#990000\">".$_FILES['userfile']['type'][$i]." not allowed. Not uploaded.</font><br><br>";
    } elseif ($size > $DGB["UploadMaxSize"]) {
	 echo "[<b>x</b>] <font color=\"#990000\">Too big. Not uploaded.</font><br><br>";
    } else {
     $name_prefix = date("Ymd-Hi-").$Topic_ID."-";
     $name_base = ereg_replace("[^a-zA-Z0-9\.-]", "", $_FILES['userfile']['name'][$i]);
	 $name = $name_prefix.$name_base;
     
     // make sure the filename is unique / not exist
     for ($n=2; file_exists($DGB["UPLOAD_PATH"]."/".$name); $n++) {
     	$name = $name_prefix.$n."-".$name_base;
     }
	 
	 move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $DGB["UPLOAD_PATH"]."/".$name);
   $tcount++;
     echo "[<b>".($tcount)."</b>] <a class=\"linkBtn\" href=\"javascript:opener.put('".'\n(p) '.($tcount).'\n'."[img]".$FULL_UPLOAD_PATH."/".$name."[/img]".'\n'."')\"><font color=\"#006600\">".$name."</font></a><br><br>";
	 $count++;
    }
   }
  }
 ?>
 <?if ($count == 0) echo "&nbsp;<font color=\"#990000\">No picture has been selected.</font>";?> 
 </td>
 </tr>
 <tr align="center">
 <td><hr><font size=2>
 <b>uploaded <?=$count;?>
 <? if ($count <= 1) {?>
 picture.</b>
 <?} else {?>
  pictures.</b>
 <?}?><br>
 total <?=$tcount;?>
 <? if ($tcount <= 1) {?>
 picture</b>
 <?} else {?>
  pictures</b>
 <?}?> uploaded.</font>
 <hr>
 <a class="linkBtn" href="upload_ui.php?Topic_ID=<?=$Topic_ID;?>&tcount=<?=$tcount;?>"><font size=2>upload more</font></a> |
 <a class="linkBtn" href="javascript:window.close()"><font size=2>finish</font></a></td>
</tr>
</table>
<?} else {?>
<h2 align="center">Sorry, upload feature is not enabled.</h2>
<?}?>

</body>
</html>
